package com.juyu.reader.pc.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.juyu.reader.pc.model.Book;
import com.juyu.reader.pc.model.User;
import com.juyu.reader.pc.service.BookService;
import com.juyu.reader.pc.service.SolrService;
import com.juyu.reader.pc.service.UserService;
import com.juyu.reader.pc.util.StringUtil;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/test")
public class TestController extends BaseController{
	@Autowired
	private SolrService solrService;
	@Autowired
	private BookService bookService;
	@Autowired
	private UserService userSerivce;
	
	/**
	 * 查询出现在数据库中存放的书籍，建立solr库索引，用来实现搜索功能
	 * @param request
	 * @return
	 */
	@RequestMapping("/solrFindBook")
	public ModelAndView solrFindBook(HttpServletRequest request) {
		Book book = new Book();
		book.setOffset(0);
		book.setLimit(10000);
		book.setBookFlag(-1);
		List<Book> bookList = bookService.findBook(book);
		System.out.println(bookList.size());
		try {
			solrService.createIndex(bookList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("ok");
		return this.packModeAndView(request, "/common/error", "bookList.size()", null);
	}
	@RequestMapping("/insertUser")
	public ModelAndView insertUser(HttpServletRequest request) throws ClientProtocolException, IOException {
		String accessToken = "22__vVt8jyMIAWsrJgREU3MMeD7FuIUkglDhLuv3T4C5Jzryfn-rr6uALNYVYPgG7ykp0lMxnHNKNl9acqgza4hoZOC-boFkDnSDc_920V2wN4";
		String openid = "obyaK0xv2cALOfdWpx1HRiEU_Eo0";
		HttpGet httpGet = new HttpGet("https://api.weixin.qq.com/sns/userinfo?access_token=" + accessToken
				+ "&openid=" + openid);
		CloseableHttpClient httpclient = HttpClients.createDefault();
		CloseableHttpResponse closresponse = httpclient.execute(httpGet);
		HttpEntity entity2 = closresponse.getEntity();
		String userInfoResult = EntityUtils.toString(entity2, "utf-8");
		JSONObject userInfoObj = JSONObject.fromObject(userInfoResult);
		String nickname = userInfoObj.optString("nickname", "");
		String imageUrl = userInfoObj.optString("headimgurl", "");
		String unionid = userInfoObj.optString("unionid", "");
		// 检查用户是否已经注册
		User updateUser = new User();
		updateUser.setUserOpenId(openid);
		updateUser.setUserUnionId(unionid);
		updateUser.setUserType(2);
		updateUser.setUserStatus(1);
		updateUser.setUserPlatform(1);
		updateUser.setUserAccessToken(accessToken);
		updateUser.setUserChannelNo((String) request.getSession().getAttribute("channelNo"));
		updateUser.setUserNo(StringUtil.createUserNo());
		updateUser.setUserIsUnion(1);
		updateUser.setUserNickname(nickname);
		int opResult = userSerivce.insertUser(updateUser);
		return this.packModeAndView(request, "/common/error", opResult+"", null);
	}
}
